package com.team.teamproject.ui.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.team.teamproject.R;
import com.team.teamproject.bean.HotWords;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class SearchActivity extends AppCompatActivity {

    @InjectView(R.id.ed_search)
    EditText edSearch;
    @InjectView(R.id.tv_quxiao)
    TextView tvQuxiao;
    //搜索热词
    String hotWords = "http://api.liwushuo.com/v2/search/hot_words";
    @InjectView(R.id.tv)
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_search);
        ButterKnife.inject(this);
        //搜索----
        Search();
        tvQuxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        hotWords();
    }

    private void Search() {
        edSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    //获取edittext输入
                    String msg = edSearch.getText().toString().trim();
                    Log.i("aaa", "要搜索东西的msg---->" + msg);
                    try {
                        String s = URLEncoder.encode(msg, "UTF-8");
                        Intent intent = new Intent();
                        intent.putExtra("msg",s);
                        intent.setClass(SearchActivity.this,SearchToDetail.class);
                        startActivity(intent);
                        return true;
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                    return false;
            }
        });
    }

    public void hotWords() {
        new Retrofit.Builder()
                .baseUrl("http://api.liwushuo.com/v2/search/")
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(Rx_rf_Data_HomePage.class)
                .hotWords()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<HotWords>() {
                    @Override
                    public void call(HotWords hotWords) {
                        List<String> hot_words = hotWords.getData().getHot_words();
                        tv.setText(hot_words.toString());
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable t) {
                       tv.setText(t.getMessage());
                    }
                });


    }

}
